﻿Imports System.Data.Linq
Imports System.Linq
Public Class CLineContController
    Dim objDataContext As New TanNamChinhDataContext
    Public Function SelectAllRecord() As IQueryable(Of tblLineCont)
        Try
            Return (From l In objDataContext.tblLineConts Where l.IsActive = True Select l)
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    Public Function SelectContsByLine(ByVal idLine As Integer, ByVal _idContType As Integer, ByVal sContNo As String) As IQueryable
        Try
            'Đọc dữ liệu
            Dim Rowss = SelectAllRecord()
            'Xử lý lọc
            If (idLine > 0) Then
                Rowss = Rowss.Where(Function(l) l.ID_Line = idLine)
            End If
            If (_idContType > 0) Then
                Rowss = Rowss.Where(Function(l) l.ID_ContType = _idContType)
            End If
            If (sContNo <> "") Then
                Rowss = Rowss.Where(Function(l) l.ContNo.Contains(sContNo))
            End If

            'Lấy kết quả và trả về
            Dim q As IQueryable = (From c In Rowss
                                   Select New With {c.ID_LineCont, c.ContNo, c.tblContType.ContTypeName, c.tblLine.LineName}).OrderBy(Function(c) c.ContNo)


            Return (q)

            'Return From c In objDataContext.tblLineConts
            '       Where c.ID_Line = idLine And c.IsActive = True
            '       Select New With {c.ID_LineCont, c.ContNo, c.tblContType.ContTypeName}
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    Public Function SelectContNoByLine(ByVal idLine As Integer) As Array
        Try
            Return (From c In objDataContext.tblLineConts
                   Where c.ID_Line = idLine And c.IsActive = True
                   Select c.ContNo).ToArray()
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    Public Function SelectRecordByID(ByVal _id As Integer) As tblLineCont
        Try
            Return (From r In objDataContext.tblLineConts
                    Where r.ID_LineCont = _id And r.IsActive = True
                    Select r).Single
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    Public Function SelectRecordByContNoAndLine(ByVal _idLine As Integer, ByVal contNo As String) As tblLineCont
        Try
            Return (From r In objDataContext.tblLineConts
                    Where r.ID_Line = _idLine And r.ContNo = contNo And r.IsActive = True
                    Select r).Single
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    Public Function InsertRecord(ByVal LocalTable As tblLineCont) As Integer
        Try
            objDataContext.tblLineConts.InsertOnSubmit(LocalTable)
            objDataContext.SubmitChanges()
            Return LocalTable.ID_LineCont
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Sub UpdateRecord(ByVal LocalTable As tblLineCont)
        Try
            objDataContext.tblLineConts.Attach(LocalTable)
            objDataContext.Refresh(RefreshMode.KeepCurrentValues, LocalTable)
            objDataContext.SubmitChanges(ConflictMode.ContinueOnConflict)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    Public Sub UpdateRecordWithoutAttach(ByVal LocalTable As tblLineCont)
        Try
            objDataContext.Refresh(RefreshMode.KeepCurrentValues, LocalTable)
            objDataContext.SubmitChanges(ConflictMode.ContinueOnConflict)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Public Sub DeleteRecord(ByVal _id As Integer)
        Try
            objDataContext.tblLineConts.DeleteOnSubmit( _
              (From r In objDataContext.tblLineConts _
               Where r.ID_LineCont = _id).Single)
            objDataContext.SubmitChanges()
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    Public Function CheckExists(ByVal sContNo As String, ByVal id As Integer, ByVal idLine As Integer) As Boolean
        'cach 1
        Return objDataContext.tblLineConts.Any(Function(l) l.ContNo = sContNo And l.ID_LineCont <> id And l.ID_Line = idLine And l.IsActive = True)
    End Function
    Public Sub New()
        objDataContext = New TanNamChinhDataContext
    End Sub
    Public Sub New(ByVal _obj As TanNamChinhDataContext)
        objDataContext = _obj
    End Sub
End Class
